/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author ihabali
 */
@Entity
@Table(name = "QUESTIONS")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Questions.findAll", query = "SELECT q FROM Questions q"),
    @NamedQuery(name = "Questions.findByQuestionid", query = "SELECT q FROM Questions q WHERE q.questionid = :questionid"),
    @NamedQuery(name = "Questions.findByQuestion", query = "SELECT q FROM Questions q WHERE q.question = :question"),
    @NamedQuery(name = "Questions.findByQuestiontypeid", query = "SELECT q FROM Questions q WHERE q.questiontypeid = :questiontypeid"),
    @NamedQuery(name = "Questions.findByDescription", query = "SELECT q FROM Questions q WHERE q.description = :description"),
    @NamedQuery(name = "Questions.findByAnswer", query = "SELECT q FROM Questions q WHERE q.answer = :answer"),
    @NamedQuery(name = "Questions.findByDistractors", query = "SELECT q FROM Questions q WHERE q.distractors = :distractors"),
    @NamedQuery(name = "Questions.findBySavedanswer", query = "SELECT q FROM Questions q WHERE q.savedanswer = :savedanswer")})
public class Questions implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "QUESTIONID")
    private Integer questionid;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 5000)
    @Column(name = "QUESTION")
    private String question;
    @Basic(optional = false)
    @NotNull
    @Column(name = "QUESTIONTYPEID")
    private int questiontypeid;
    @Size(max = 255)
    @Column(name = "DESCRIPTION")
    private String description;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "ANSWER")
    private String answer;
    @Size(max = 5000)
    @Column(name = "DISTRACTORS")
    private String distractors;
    @Size(max = 500)
    @Column(name = "SAVEDANSWER")
    private String savedanswer;
    @ManyToMany(mappedBy = "questionsList")
    private List<Exam> examList;

    public Questions() {
    }

    public Questions(Integer questionid) {
        this.questionid = questionid;
    }

    public Questions(Integer questionid, String question, int questiontypeid, String answer) {
        this.questionid = questionid;
        this.question = question;
        this.questiontypeid = questiontypeid;
        this.answer = answer;
    }

    public Integer getQuestionid() {
        return questionid;
    }

    public void setQuestionid(Integer questionid) {
        this.questionid = questionid;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public int getQuestiontypeid() {
        return questiontypeid;
    }

    public void setQuestiontypeid(int questiontypeid) {
        this.questiontypeid = questiontypeid;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public String getDistractors() {
        return distractors;
    }

    public void setDistractors(String distractors) {
        this.distractors = distractors;
    }

    public String getSavedanswer() {
        return savedanswer;
    }

    public void setSavedanswer(String savedanswer) {
        System.out.println("answer saved" + savedanswer );
        this.savedanswer = savedanswer;
    }

    @XmlTransient
    public List<Exam> getExamList() {
        return examList;
    }

    public void setExamList(List<Exam> examList) {
        this.examList = examList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (questionid != null ? questionid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Questions)) {
            return false;
        }
        Questions other = (Questions) object;
        if ((this.questionid == null && other.questionid != null) || (this.questionid != null && !this.questionid.equals(other.questionid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Entities.Questions[ questionid=" + questionid + " ]";
    }
    
    
    public Map<String,String> getDistractorsMap (){
       String[] distarctorsArray = this.distractors.split(",");
       ArrayList<String> temp =  new ArrayList();
       
       for(String s : distarctorsArray){
            temp.add(s);
        }
       if(this.questiontypeid != 1)
          Collections.shuffle(temp);
       LinkedHashMap<String,String> map =  new LinkedHashMap<>();
        
        for(String s : temp){
            map.put(s, s);
        }
        return map;
    }
    
    
    public String checkAnswer(){
        if(savedanswer ==null){
            return "wrong.png";
        }
       if(answer.trim().equals(savedanswer.trim())){
           return "correct.png" ;
       }else{
           return "wrong.png";
       }
       
    }    
}
